// 遍历对象
export function forEachValue(obj, fn) {
  // obj: {a:fns, b:fns}  ==>  fn(fns,a)
  Object.keys(obj).forEach((key) => fn(obj[key], key));
}

export function isPromise(val) {
  return val && val.then === "function";
}

export function isObject(obj) {
  return obj !== null && typeof obj === "object";
}

export function find(list, f) {
  return list.filter(f)[0];
}
// 克隆
export function deepCopy(obj, cache = []) {
  if (obj === null || typeof obj !== "object") {
    return obj;
  }
  const hit = find(cache, (c) => c.original === obj);
  if (hit) {
    return hit.copy;
  }

  const copy = Array.isArray(obj) ? [] : {};

  cache.push({
    original: obj,
    copy,
  });

  Object.keys(obj).forEach((key) => {
    copy[key] = deepCopy(obj[key], cache);
  });

  return copy;
}
